package com.uchengwang.ui.cm_ui.cycle.fm;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnItemLongClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.ui.base_ui.basefm.BaseFragment;
import com.uchengwang.ui.cm_ui.cycle.adapter.TalentInfosAdapter;
import com.uchengwang.ui.cm_ui.cycle.bean.ArticleBean;
import com.uchengwang.ui.cm_ui.cycle.bean.H5ReportBean;
import com.uchengwang.ui.cm_ui.cycle.http.CycleHttpUtils;
import com.uchengwang.ui.cm_ui.cycle.ui.LoadCycleWebHtmlActivity;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by U城网 on 2018/6/14.
 */

public class FragmentTalentInfos extends BaseFragment{
    private View view;
    private LRecyclerView rv;
    private TalentInfosAdapter infosAdapter;
    private int page = 1, totalPage;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;

    private int type;//1最热  2最新 3本校
    private long cycleItemId;//主题id


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        setLayoutId(R.layout.fm_layout_talent_infos);
        view = super.onCreateView(inflater, container, savedInstanceState);

        EventBus.getDefault().register(this);

        initView();

        return view;
    }

    private void initView(){
        rv = view.findViewById(R.id.fm_rv);
        initData();
    }

    private void initData(){
        type = getArguments().getInt(BaseConfig.TYPE);
        cycleItemId = getArguments().getLong(BaseConfig.CYCLE_ITEM_ID);

        infosAdapter = new TalentInfosAdapter(getContext());
        infosAdapter.setBottomView(rv);
        rv.setLayoutManager(new LinearLayoutManager(getContext()));

        mLRecyclerViewAdapter = new LRecyclerViewAdapter(infosAdapter);
        rv.setAdapter(mLRecyclerViewAdapter);


        //设置头部加载颜色
        rv.setHeaderViewColor(R.color.comment_color, R.color.uni_title_bar ,R.color.white);
        //设置底部加载颜色
        rv.setFooterViewColor(R.color.comment_color, R.color.uni_title_bar ,R.color.white);
        //设置底部加载文字提示
        rv.setFooterViewHint("拼命加载中","已经全部为你呈现了","网络不给力啊，点击再试一次吧");

        rv.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getData(true);
            }
        });

        rv.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                if (page <= totalPage){
                    //当前请求页大于总页数，不再请求
                    getData(false);
                } else {
                    rv.setNoMore(true);
                }
            }
        });

        mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(FragmentTalentInfos.this.getContext(), LoadCycleWebHtmlActivity.class);
                intent.putExtra(BaseConfig.URL_TITLE, infosAdapter.getData().get(position).getArticleTitle());
                intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/article/" + infosAdapter.getData().get(position).getId());
                FragmentTalentInfos.this.getContext().startActivity(intent);
            }

        });

        mLRecyclerViewAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public void onItemLongClick(View view, int position) {
            }
        });

        rv.setRefreshProgressStyle(ProgressStyle.LineSpinFadeLoader);
        rv.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);

        rv.forceToRefresh();


    }

    /**
     * 刷新数据
     * */
    public void onEvent(H5ReportBean h5ReportBean) {
        if (h5ReportBean != null){
            List<ArticleBean> data = infosAdapter.getData();
            for (ArticleBean articleBean : data){
                if (articleBean.getId() == h5ReportBean.getReferId()){
                    data.remove(articleBean);
                    infosAdapter.notifyDataSetChanged();
                    break;
                }
            }

        }
    }


    private void getData(final boolean isRefresh){
        CycleHttpUtils.getUtils().getTalentInfos(getActivity(), cycleItemId, page, 10, type, new OnHttpResultListener<ArticleBean>() {
            @Override
            public void onResult(int status, String msg, int total, List<ArticleBean> list) {

                if (isRefresh){
                    rv.refreshComplete(10);
                    mLRecyclerViewAdapter.notifyDataSetChanged();
                }else {
                    rv.refreshComplete(10);// REQUEST_COUNT为每页加载数量
                }

                if (list != null){
                    totalPage = total;
                    if (isRefresh){
                        infosAdapter.setData(list);
                    }else {
                        infosAdapter.getData().addAll(list);
                        infosAdapter.notifyDataSetChanged();
                    }
                }
            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
